﻿ 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Utils.Common;
using Utils.Common.Enum;
using ZK.BLL.Config;
using ZK.MyControl;
using ZK.Entity.Config;

namespace ZK.Client.WinForm
{
    public partial class FrmSystemSet : Form
    {
        public FrmSystemSet()
        {
            InitializeComponent();
            AddPropertys();
        }



     
        private void FrmSystemSet_Load(object sender, EventArgs e)
        {
            //MultiLanguage.LoadLanguage(this, this.GetType());
        }

        //private void LanguageChange()
        //{
        //    bool cn = false;
        //    if (Properties.Settings.Default.DefaultLanguage == "zh-CN")
        //    {
        //        cn = true;
        //    }
        //    foreach (KeyValuePair<PropertyInfo, Control> pair in keyValuePairs)
        //    {

        //        DescriptionAttribute descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(pair.Key, typeof(DescriptionAttribute));
        //        if (descriptionAttribute == null)
        //            return;
        //            if (pair.Value is TextBox)
        //        {
        //            TextBox textBox = (TextBox)pair.Value;
        //            textBox.Text = cn? descriptionAttribute.Description:pair.Key.Name;
        //                return;
        //        }
        //        if (pair.Value is MCheckBox)
        //        {
        //            MCheckBox checkBox = (MCheckBox)pair.Value;
        //            checkBox.Text = cn ? descriptionAttribute.Description : pair.Key.Name;
        //            return;

        //        }
        //    }

        //}


        SystemConfig systemConfig = SystemConfig.GetConfig();
        Dictionary<PropertyInfo, Control> keyValuePairs = new Dictionary<PropertyInfo, Control>();
        /// <summary>
        /// 添加属性
        /// </summary>
        private void AddPropertys()
        {

            int lblwidth = 250;
            PropertyInfo[] propertys = systemConfig.SystemInfoEntity.GetType().GetProperties();
            foreach (PropertyInfo property in propertys)
            {
                DescriptionAttribute descriptionAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(property, typeof(DescriptionAttribute));
                if (descriptionAttribute == null) continue;
                Panel panel = new Panel();
                panel.Height = 50;
                panel.Dock = DockStyle.Top;
                //panel.BorderStyle = BorderStyle.FixedSingle;
                string description = descriptionAttribute.Description;// 属性值  
                Label lbl = new Label();
                lbl.AutoSize = false;
                lbl.Text = description + "  ";
                lbl.Dock = DockStyle.Left;
                lbl.Width = lblwidth;
                lbl.TextAlign = ContentAlignment.MiddleRight;
                lbl.Font = this.panelMain.Font;
                panel.Controls.Add(lbl);
                lbl.BringToFront();
                object value = property.GetValue(systemConfig.SystemInfoEntity, null);

                if (property.PropertyType == typeof(bool))
                {
                    MCheckBox ckb = new MCheckBox();
                    ckb.Name = property.Name;

                  
                    if (value != null)
                    {
                        ckb.Checked = (bool)value;
                    }

                    ckb.Location = new Point(lblwidth, 15);
                    panel.Controls.Add(ckb);
                    ckb.BringToFront();

                    keyValuePairs.Add(property, ckb);

                }
                else if (property.PropertyType.IsEnum)
                {
                    ComboBox comboBox = new ComboBox();
                    comboBox.Name = property.Name;
                    //comboBox.DataSource = System.Enum.GetNames(property.PropertyType);

                    foreach (var name in Enum.GetNames(property.PropertyType))
                    {
                        comboBox.Items.Add(name);
                    }
                    comboBox.Width =200;
                    comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
                    comboBox.Location = new Point(lblwidth, 10);
                    comboBox.Font = this.panelMain.Font;
                    panel.Controls.Add(comboBox);
                    comboBox.BringToFront();
                    keyValuePairs.Add(property, comboBox);

                    if (value != null)
                    {
                        comboBox.SelectedIndex = (int)Enum.Parse(property.PropertyType, value.ToString());
                    }
                }
                else
                {
                    TextBox textBox = new TextBox();
                    textBox.Name = property.Name;
                 
                    if (value != null)
                    {
                        textBox.Text = value.ToString();
                    }
                    textBox.Width = 200;
                    //textBox.Dock = DockStyle.Left;
                    textBox.Location = new Point(lblwidth, 10);
                    textBox.Font = this.panelMain.Font;

                    panel.Controls.Add(textBox);
                    textBox.BringToFront();
                    keyValuePairs.Add(property, textBox);
                }

                panelMain.Controls.Add(panel);
                panel.BringToFront();
            }
        }


        /// <summary>
        /// 修改属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            foreach (KeyValuePair<PropertyInfo, Control> pair in keyValuePairs)
            {
                if (pair.Value is TextBox)
                {
                    if (!SetModelValue(pair.Key, pair.Value.Text.Trim(), systemConfig.SystemInfoEntity))
                        return;
                }
                if (pair.Value is MCheckBox)
                {
                    MCheckBox checkBox = (MCheckBox)pair.Value;
                    if (!SetModelValue(pair.Key, checkBox.Checked.ToString(), systemConfig.SystemInfoEntity))
                        return;

                }

                if (pair.Value is ComboBox)
                {
                    ComboBox checkBox = (ComboBox)pair.Value;


                    if (!this.SetModelValue(pair.Key, Enum.Parse(pair.Key.PropertyType, checkBox.SelectedItem.ToString()), systemConfig.SystemInfoEntity))
                        return;
                }
            }

            if (systemConfig.EditConfig())
            {
                MessageBox.Show("保存成功");
            }
            else
            {
                MessageBox.Show("保存失败");
            }
        }



        /// <summary>
        /// 设置类中的属性值
        /// </summary>
        /// <param name="FieldName"></param>
        /// <param name="Value"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SetModelValue(PropertyInfo property, object value, object obj)
        {
            try
            {
                object v = Convert.ChangeType(value, property.PropertyType);
                property.SetValue(obj, v, null);
                return true;
            }
            catch (Exception ex)
            {
                string description = ((DescriptionAttribute)Attribute.GetCustomAttribute(property, typeof(DescriptionAttribute))).Description;// 属性值  
                MessageBox.Show(string.Format("保存[{0}]发生错误,错误原因:{1}", description, ex.Message));
                return false;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

      
    }
}
